﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Unity.Utility;

namespace Artech.EnterLibExt.ExceptionHandling
{
    public class ExceptionFilterTableEntry
    {
        private IExceptionFilter filter;
        private string filterRegistrationName;
        private List<IExceptionHandler> exceptionHandlers;
        private IEnumerable<string> handlerRegistrationNames;

        public IExceptionFilter Filter
        {
            get
            {
                if (null == filter)
                {
                    filter = EnterpriseLibraryContainer.Current.GetInstance<IExceptionFilter>(filterRegistrationName);
                }
                return filter;
            }
        }
        public IEnumerable<IExceptionHandler> ExceptionHandlers
        {
            get
            {
                if (null == exceptionHandlers)
                {
                    exceptionHandlers = new List<IExceptionHandler>();
                    foreach (string handlerName in handlerRegistrationNames)
                    { 
                        exceptionHandlers.Add(EnterpriseLibraryContainer.Current.GetInstance<IExceptionHandler>(handlerName));
                    }
                }
                return exceptionHandlers;
            }
        }
        public int Priority { get; private set; }

        public ExceptionFilterTableEntry(string filterRegistrationName, int priority, IEnumerable<string> handlerRegistrationNames)
        {
            Guard.ArgumentNotNullOrEmpty(filterRegistrationName, "filterRegistrationName");
            Guard.ArgumentNotNull(handlerRegistrationNames, "handlerRegistrationNames");

            this.filterRegistrationName = filterRegistrationName;
            this.handlerRegistrationNames = handlerRegistrationNames;
            this.Priority = priority;
        }
    }
}